;(function(factory) {
    window.driverStorage = factory();
})(function () {
    'use strict';

    var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

    function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

    var events = {};
    var _ready = false;

    var DriverStorage = function () {
        function DriverStorage() {
            _classCallCheck(this, DriverStorage);
        }

        _createClass(DriverStorage, [{
            key: 'setItem',
            value: function setItem(key, value) {
                var self = this;
                self[key] = value;
                driver.update('storage', { key: key, value: value });
            }
        }, {
            key: 'getItem',
            value: function getItem(key) {
                return driver.table('storage').queryByKey(key).then(function (kv) {
                    return kv && kv.value;
                });
            }
        }, {
            key: 'removeItem',
            value: function removeItem(key) {
                delete this[key];
                return driver.delete('storage', key);
            }
        }, {
            key: 'ready',
            value: function ready(fn) {
                if (_ready) {
                    fn.call(this, this);
                    return;
                }
                events.ready = fn;
            }
        }]);

        return DriverStorage;
    }();

    var driverStorage = new DriverStorage();
    var driver = new Driver('_distortion');
    driver.on('success', function () {
        driver.select('storage').then(function (arr) {
            arr.forEach(function (kv) {
                return driverStorage[kv.key] = kv.value;
            });
            _ready = true;
            events.ready && events.ready.call(events, driverStorage);
        });
    });

    driver.open().then(function () {
        driver.createTable({
            name: 'storage',
            primaryKey: 'key',
            indexes: [{ name: 'name_index', column: 'key', unique: true }]
        });
    });

    return driverStorage;
})